package com.zte.claa.inficombo.csa.app.service.mqttproxy.mqttclient;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
// import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MqttClientCallBack implements MqttCallbackExtended {
	// logger
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private String clientId;
	
	private MqttPubSubClient pubClient = null;
	
	public MqttClientCallBack(String clientId, MqttPubSubClient pubClient){
		this.clientId = clientId;
		this.pubClient = pubClient;
	}

	@Override
	public void connectionLost(Throwable t) {
		logger.warn("MQTT client connection lost and reconnect: clientid={}", this.clientId);
		if(this.pubClient != null){
			this.pubClient.connectToSvr();
		}
	}

	@Override
	public void deliveryComplete(IMqttDeliveryToken token) {
		logger.info("Mqtt deliveryComplete: "+token.toString()); 
	}

	@Override
	public void messageArrived(String topic, MqttMessage msg) throws Exception {
		logger.info("messageArrived: topic={}, msg={}", topic, msg.toString());		
	}

	@Override
	public void connectComplete(boolean reconnect, String svrURI) {
		// 避免broker频繁重启后，subscribe接收不到消息的问题》
		this.pubClient.subscribe();
	}

}
